package com.kintreda.ieco_server.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 字符工具类
 * @author HuangZhongYao
 *
 */
public class StringUtil {
	
	private final static String[] seed = new String[] { "A", "B", "C", "D",
		"E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
		"R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
		"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
		"r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3",
		"4", "5", "6", "7", "9"};
	
	
	private final static String[] capitalSeed = new String[] { "A", "B", "C", "D",
		"E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
		"R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3",
		"4", "5", "6", "7", "9"};
	
	private final static String[] sendNum = {"0", "1", "2", "3","4", "5", "6", "7", "9"};
	
	public static String getRandom(int length) {
		int index;
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < length; i++) {
			index = (int) (new Random().nextFloat() * seed.length);
			result.append(seed[index]);
		}
		return result.toString();
	}
	
	public static String getRandomNum(int length) {
		int index;
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < length; i++) {
			index = (int) (new Random().nextFloat() * sendNum.length);
			result.append(sendNum[index]);
		}
		return result.toString();
	}
	
	
	public static String inputStream2String(InputStream in) throws IOException {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		return out.toString();
	}
	
	/*************************************************************
	 * 17位数
	 * @return
	 */
	public static String getLongUID() {
		
		return System.currentTimeMillis() + "" + StringUtil.getRandomNum(4);
	}
	
	/*************************************************************
	 * 18位数
	 * @return
	 */
	public static String getLongUID(String prefix) {
		
		return prefix + System.currentTimeMillis() + "" + StringUtil.getRandomNum(4);
	}

	public static String getRandomAndTimestamp(int length) {
		String time = new Date().getTime()+"";
		time = time.substring(time.length()-6);
		int index;
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < length; i++) {
			index = (int) (new Random().nextFloat() * seed.length);
			result.append(seed[index]);
		}
		return result.toString()+time;
	}
	
	/*************************************************************
	 * add HuangZhongYao 2014-07-04
	 * 将小写金额转换为大写中文金额
	 * @param
	 * @return
	 */
	public static String convertToCapitalMoney(BigDecimal _amount){ 
		
		String str = ""; 
		String number[] = new String[]{"零 ","壹 ","贰 ","叁 ","肆 ","伍 ","陆 ","柒 ","捌 ","玖 "}; 
		String units[] = new String[]{"圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"}; 
		
		double temp = _amount.doubleValue();
		double amount = _amount.doubleValue();
		if(temp<0) {
			temp = temp * -1;
		}
		if(amount < 0) {
			amount = amount * -1;
		}
		int i = 0; 
		
		//整数部分 
		while(temp >= 10){ 
			int n = (int)(temp % 10); 
			temp = temp / 10L; 
			str = str.trim()+units[i++].trim()+number[n].trim(); 
		}
		
		str = str.trim()+units[i++].trim()+number[(int)temp].trim(); 
		StringBuffer sb = new StringBuffer(str.trim()); 
		sb.reverse();
		str = sb.toString(); 
		
		//小数部分 
		int dolt1 = (int)((amount+0.005)*100%10);//分 
		int dolt2 = ((int)((amount+0.005)*100%100))/10;//角 
		
		if(dolt1 == 0 && dolt2 == 0) {
			str = str +"整";
		} else if(dolt1 != 0) { 
			if(dolt2 != 0) {
				str = str + number[dolt2].trim()+"角"+number[dolt1].trim()+"分"; 
			} else str = str + "零"+number[dolt1].trim()+"分"; 
		} else if(dolt2 != 0){ 
			str = str + number[dolt2].trim()+"角";
		} 
		
		//去除多余的汉字并在合适的位置加上零 
	    while(str.indexOf("零亿")!= -1 ||str.indexOf("零万")!= -1 ||str.indexOf("零仟")!= -1 
		    ||str.indexOf("零佰")!= -1 ||str.indexOf("零拾")!= -1 
		    ||str.indexOf("零圆")!= -1 ) { 
	    	
			    str = str.replaceAll("零亿", "亿"); 
			    str = str.replaceAll("零万", "万"); 
			    str = str.replaceAll("零仟", "零"); 
			    str = str.replaceAll("零佰", "零"); 
			    str = str.replaceAll("零拾", "零"); 
			    
			    str = str.replaceAll("零零零", "零");
			    str = str.replaceAll("零零", "零");
			   
			    if(str.startsWith("零圆")) 
			    	str = str.replaceAll("零圆", ""); 
			    else str = str.replaceAll("零圆", "元"); 
	    }
	    
	    if(str.indexOf("亿万") != -1){ 
	    	str = str.replaceAll("亿万", "亿"); 
	    } 
	    
	    if(str.indexOf("万仟") != -1){ 
	    	str = str.replaceAll("万仟", "万"); 
	    } 
	    
	    if(str.indexOf("零零") != -1){ 
	    	str = str.replaceAll("零零", "零"); 
	    }
	    
	    if(_amount.floatValue() < 0) {
	    	str = "负" + str;
	    }
		
	    return str; 
	}
	
	
	/*************************************************************
	 * 将字符串转换未ASCII码
	 * @param value
	 * @return
	 */
	public static String stringToAscii(String value) {  
		
	    StringBuffer sbu = new StringBuffer();  
	    char[] chars = value.toCharArray();   
	    for (int i = 0; i < chars.length; i++) {  
	        if(i != chars.length - 1){  
	            sbu.append((int)chars[i]).append("-");  
	        } else {  
	            sbu.append((int)chars[i]);  
	        }  
	    }  
	    
	    return sbu.toString();  
	} 
	
	
	/*************************************************************
	 * 判断是否在List中
	 * @param source
	 * @return
	 */
	public static boolean in(String source,List<String> strList) {
		if(null == strList || strList.isEmpty()) return false;
		for(String i : strList) {
			if(i.equals(source)) {
				return true;
			}
		}
		
		return false;
	}
	
	
	/*************************************************************
	 * 验证字符串是否为数值
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		
		Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); 
	    return pattern.matcher(str).matches();
	}
	
	/*************************************************************
	 * 主键id 时间
	 * @return
	 */
	public static String createId() {
		return DateUtil.getDate(DateUtil.YYYYMMDDHHMMSS) + Math.round(Math.random() * 8999.0D + 1000.0D);
	}
	
	
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	/*************************************************************
	 * 
	 * 获取固定前缀单号和无序单号
	 * @param is   true 固定前缀   false 无序单号  i为 null
	 * @param areaCode    区号
	 * @param i   无序单号位数  areaCode为null
	 * @return
	 */
	public static String getSerialNumber(boolean is,String areaCode,Integer i){
		int index;
		if (is) {
			String prefix = "YSPS"+areaCode;
			String ymd = new SimpleDateFormat("YYYYMMdd").format(new Date());
			String mmss = new SimpleDateFormat("mmss").format(new Date());
			return prefix+ymd+mmss.toString();
		}
		
		
		StringBuffer serial = new StringBuffer();
		for (int j = 0; j < i; j++) {
			 
			index = (int) (new Random().nextFloat() * capitalSeed.length);
			serial.append(capitalSeed[index]);
		}
		
		return serial.toString();
		
		
	}
	
	

	
	
}
